<?php
require_once(dirname(dirname(dirname(__FILE__))) . '/app.php');

need_manager(true);

$s = isset($_GET['s']) ? strval($_GET['s']) : null;
$ts = $s ? '_' . $s : null;

$system = Table::Fetch('system', 1);

if ($s == 'a'){
	$root = DIR_TEMPLATE;
    $template_id = trim(strval($_GET['id']));
	$template_id = str_replace('\\', '_', $template_id);
	$template_id = str_replace('/', '_', $template_id);
	
	if ( $_POST ) {
		$path = "{$root}/{$template_id}";
		if(is_writable($path) && !is_dir($path) && is_file($path)) {
			$flag = file_put_contents($path, stripslashes(trim($_POST['content'])));
		}
		if ( $flag ) {
	        log_admin('system', '编辑模板成功:'.$template_id);
			Session::Set('notice', "模板 {$template_id} 修改成功");
		} else {
	        log_admin('system', '编辑模板失败:'.$template_id);
			Session::Set('error', "模板 {$template_id} 修改失败");
		}
		redirect(WEB_ROOT . "/manage/system/sms.php?id={$template_id}&s={$s}");
	}
	
	$may = array(
		'manage_tpl_smssubscribe.html' => '短信订阅',
		'manage_tpl_smscoupon.html' => '短信优惠券',
		'manage_tpl_smsvoucher.html' => '短信商户券',
		'manage_tpl_smsexpress.html' => '快递通知',		
		'manage_tpl_usecoupon.html' => '消费通知'
    );
    
    if ($template_id == 'manage_tpl_smssubscribe.html'){
    	$varbs = array(
		"\{\$INI\[\'system\'\]\[\'sitename\'\]\}" => "网站名称",
		"\{\$team\[\'title\'\]\}" => "项目名称"
    	);
    }
    
    if ($template_id == 'manage_tpl_smscoupon.html'){
    	$varbs = array(
		"\{\$INI\[\'system\'\]\[\'sitename\'\]\}" => "网站名称",
		"\{\$coupon\[\'name\'\]\}" => "商品名称",
		"\{\$coupon\[\'id\'\]\}" => "优惠券编号",
		"\{\$coupon\[\'secret\'\]\}" => "优惠券密码",
		"\{\$coupon\[\'end\'\]\}" => "优惠券有效期",
		"\{\$team\[\'flv\'\]\}" => "项目自定义内容",
    	);
    }
    
    if ($template_id == 'manage_tpl_smsvoucher.html'){
    	$varbs = array(
		"\{\$INI\[\'system\'\]\[\'sitename\'\]\}" => "网站名称",
		"\{\$voucher\[\'name\'\]\}" => "商品名称",
		"\{\$voucher\[\'code\'\]\}" => "商户券编号",
		"\{\$voucher\[\'end\'\]\}" => "优惠券有效期",
		"\{\$team\[\'flv\'\]\}" => "项目自定义内容",
    	);
    }
    
    if ($template_id == 'manage_tpl_smsexpress.html'){
    	$varbs = array(
    	"\{\$INI\[\'system\'\]\[\'sitename\'\]\}" => "网站名称",
    	"\{\$team\[\'product\'\]\}" => "商品名称",
		"\{\$express_name\}" => "快递名称",
		"\{\$express_no\}" => "快递单号",
    	);
    }
    
     if ($template_id == 'manage_tpl_usecoupon.html'){
    	$varbs = array(
    	"\{\$INI\[\'system\'\]\[\'sitename\'\]\}" => "网站名称",
		"\{\$coupon\[\'name\'\]\}" => "商品名称",
		"\{\$coupon\[\'id\'\]\}" => "优惠券编号",
		"\{\$coupon\[\'use\'\]\}" => "消费时间",
    	);
    }
    
	$template_file =  "{$root}/{$template_id}";
	if (file_exists($template_file) && !is_dir($template_file)) {
		$content = trim(file_get_contents( "{$root}/{$template_id}" ));
	} else {
		$template_id = null;
	}
}

if ($_POST) {
	unset($_POST['commit']);
	$INI = Config::MergeINI($INI, $_POST);
	$INI = ZSystem::GetUnsetINI($INI);
	save_config();

	$value = Utility::ExtraEncode($INI);
	$table = new Table('system', array('value'=>$value));
	if ( $system ) $table->SetPK('id', 1);
	$flag = $table->update(array( 'value'));
    log_admin('system', '编辑短信设置');
	Session::Set('notice', '更新系统信息成功');
	redirect(WEB_ROOT.'/manage/system/sms.php');	
}

include template("manage_system_sms{$ts}");

function current_system_sms($s=null) {
	global $city;
	$filter = array(
		'a' => '短信模板'
	);
	$a['/manage/system/sms.php'] = '基本选项';
	foreach($filter AS $id=>$name) {
		$a["/manage/system/sms.php?s={$id}"] = $name;
	}
	$l = '/manage/system/sms.php';
	if ($s) $l = "/manage/system/sms.php?s={$s}";
	return current_link($l, $a, true);
}
